﻿using System;
using CrateMayhem.Common.Data;
using CrateMayhem.Common.Model;

namespace CrateMayhem.Common.Services
{
    public class GameSettingsService : IGameSettingsService
    {
        public event EventHandler ControlsSwitched;
        public event EventHandler LevelsReset;

        private readonly ISettingsRepository settingsRepository;

        public GameSettingsService(ISettingsRepository settingsRepository)
        {
            this.settingsRepository = settingsRepository;
        }

        public bool IsControlKeyPress(Func<ControlType, bool> func, ControlType control)
        {
            return func(control);
        }

        public bool IsMoveKeyPress(Func<MoveType, bool> func, MoveType move)
        {
            return func(move);
        }

        public void SwitchControls()
        {
            settingsRepository.ToggleControlsLocaton();

            if (ControlsSwitched != null)
                ControlsSwitched(this, EventArgs.Empty);
        }

        public ControlsLocationType GetControlsLocation()
        {
            return settingsRepository.GetControlsLocation();
        }

        public int FindLastLevel()
        {
            return settingsRepository.FindLastCompletedLevel();
        }

        public void ResetLevels()
        {
            settingsRepository.ResetLevels();
            
            if (LevelsReset != null) 
                LevelsReset(this, EventArgs.Empty);
        }

        public void Save(int level, string moves)
        {
            settingsRepository.Complete(level, moves);
        }
    }
}
